<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>客户航空协议运价</title>
        <% include("/common/common_css_js.html"){} %>
    </head>
    <body>
        <div style="padding-bottom: 5px;">
            <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
                <span class="separator"></span>
                <span>客户:</span>
                <div id="keyDeliveryCustomerId" name="keyDeliveryCustomerId" class="mini-autocomplete" style="width:250px;"  popupWidth="1000" textField="name" valueField="id"
                     url="${base}/pages/customer_manage/queryDeliveryCustomerByKey"  required="false" requiredErrorText="客户不能为空"
                     searchField="key"
                     emptyText="客户名称">
                    <div property="columns">
                        <div header="客户名称" field="name"></div>
                        <div header="客户简称" field="shortName"></div>
                        <div header="客户电话" field="phone"></div>
                        <div header="客户地址" field="address"></div>
                    </div>
                </div>

                <span>目的站：</span>
                <input id="keyCityId" name="keyCityId" class="mini-treeselect"
                       url="" multiSelect="false" valueField="id" textField="name"
                       parentField="pid" allowInput="true" valueFromSelect="true"
                       emptyText="请选择目的站..."
                       style="width: 200px;" expandOnLoad="0" required="false" nullItemText="请选择目的站..."/>
                <span>始发机场：</span>
                <div id="keyAirportId" name="keyAirportId" class="mini-combobox" style="width:150px;"  popupWidth="600" textField="name" valueField="id"
                     required="false" requiredErrorText="始发机场不能为空"
                     allowInput="true"
                     emptyText="始发机场名称">
                    <div property="columns">
                        <div header="ID" field="id" width="30"></div>
                        <div header="机场名称" field="name"></div>
                        <div header="简称" field="shortName"></div>
                    </div>
                </div>
                <span>航空公司：</span>
                <div id="keyAirlinesId" name="keyAirlinesId"
                     class="mini-combobox"
                     style="width:250px;"
                     popupWidth="800" textField="name" valueField="id"
                     allowInput="true" required="false" requiredErrorText="航空公司不能为空"
                     emptyText="航空公司名称">
                    <div property="columns">
                        <div header="名称" field="name"></div>
                        <div header="简称" field="shortName"></div>
                        <div header="两字码" field="twoCode"></div>
                    </div>
                </div>

                <span>班次号：</span>
                <input id="keyFlightNo" name="keyFlightNo" class="mini-textbox" style="width: 100px;"/>

                <div style="margin-top: 5px"></div>
                <% if(so.hasPermission("bCustomerAirFreightAgreement:query")){%>
                    <a class="mini-button" iconCls="icon-search" onclick="search()">查询</a>
                <% } %>

                <% if(so.hasPermission("bCustomerAirFreightAgreement:queryCndClear")){%>
                    <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>\
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("bCustomerAirFreightAgreement:add")){%>
                    <a class="mini-button" iconCls="icon-add" onclick="addFreight('add')">新增</a>
                <% } %>

                <% if(so.hasPermission("bCustomerAirFreightAgreement:edit")){%>
                    <a class="mini-button" iconCls="icon-edit" onclick="editFreight('edit')">编辑</a>
                <% } %>

                <% if(so.hasPermission("bCustomerAirFreightAgreement:delete")){%>
                    <a class="mini-button" iconCls="icon-remove" onclick="deleteFreight()">删除</a>
                <% } %>

                <% if(so.hasPermission("bCustomerAirFreightAgreement:upload")){%>
                    <a class="mini-button" iconCls="icon-upload" onclick="showUploadPanel()">上传</a>
                <% } %>
                <span class="separator"></span>
                <a href="${base!}/hymj/files/航空协议运价.rar" target="_blank">点击下载《航空协议运价》模板</a>
            </div>
        </div>

        <div class = "mini-fit">
            <div id="datagrid1" class="mini-datagrid"
                 style="width: 100%; height: 100%;"
                 url="${base}/pages/customer_manage/freight_agreement/air/queryAirFreightAgreementList" idField="id"
                 showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
                 multiSelect = "true" allowResize="true"
                 allowSortColumn = "true"
                 sortField="createTime"
                 sortOrder="desc" >
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div field="customerId" width="140" headerAlign="center" align="left" allowSort="true" data-options='{cacheName:"customer"}' renderer="commonRender">客户名称</div>
                    <div field="airportId" width="140" headerAlign="center" align="left" allowSort="true" data-options='{cacheName:"airport"}' renderer="commonRender">始发机场</div>
                    <div field="cityId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"city"}' renderer="commonRender">目的站</div>
                    <div field="airlinesId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"airlines"}' renderer="commonRender">航空公司</div>
                    <div field="flightNo" width="140" headerAlign="center" align="center" allowSort="true">班次号</div>
                    <div field="fileId" width="140" headerAlign="center" align="center" allowSort="true" renderer="fileDownRenderer">文件下载</div>
                    <div field="comment" width="200" headerAlign="center" align="center" allowSort="true">备注</div>
                    <div field="createTime" width="140" headerAlign="center" align="center" allowSort="false" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">创建时间</div>
                    <div field="createUserId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysUser"}' renderer="commonRender">创建用户</div>
                </div>
            </div>
        </div>

        <!-- 新增航空协议运价窗口 -->
        <div id="airFreightAgreementPanel" class="mini-window" title="客户空运协议运价管理" style="width:1400px;height:700px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">

            <div property="footer" style="padding:10px;text-align: right">
                <a  class="mini-button" iconCls="icon-ok" onclick="saveAirFreightAgreement()">保存</a>
                <a  class="mini-button" iconCls="icon-close" onclick="closePanel('airFreightAgreementPanel')">关闭</a>
            </div>


            <div id="freightAgreementForm" style="width: 100%">

                <input id="opFlag" name="opFlag" emptyText="操作标志"  class="mini-hidden" required="false" enabled="false" enabled="false" requiredErrorText="操作标志不能为空" />
                <input id="id" name="id" emptyText="id值"  class="mini-hidden" required="false" enabled="false" enabled="false" requiredErrorText="ID不能为空" />

                <div style="width: 100%;display: flex;flex-direction: row">
                    <div style="width: 50%">
                        <div>客户信息区</div>
                        <span>客户:</span>
                        <div id="customerId" name="customerId"
                             class="mini-combobox"
                             style="width:250px;"
                             popupWidth="800" textField="name" valueField="id"
                             allowInput="true" required="true" requiredErrorText="客户不能为空"
                             emptyText="客户名称">
                            <div property="columns">
                                <div header="客户名称" field="name"></div>
                                <div header="客户简称" field="shortName"></div>
                                <div header="客户电话" field="phone"></div>
                                <div header="客户地址" field="address"></div>
                            </div>
                        </div>

                        <span>目的站：</span>
                        <input id="cityId" name="cityId" class="mini-treeselect"
                               url="" multiSelect="false" valueField="id" textField="name"
                               parentField="pid" allowInput="true" valueFromSelect="true"
                               style="width: 200px;" expandOnLoad="0" required="true" nullItemText="请选择..." emptyText="请选择..."/>

                        <div style="margin-top: 5px"></div>

                        <span>始发机场：</span>
                        <div id="airportId" name="airportId" class="mini-combobox" style="width:150px;"  popupWidth="600" textField="name" valueField="id"
                             required="true" requiredErrorText="始发机场不能为空"
                             allowInput="true"
                             emptyText="始发机场名称">
                            <div property="columns">
                                <div header="ID" field="id" width="30"></div>
                                <div header="机场名称" field="name"></div>
                                <div header="简称" field="shortName"></div>
                            </div>
                        </div>

                        <span>航空公司：</span>
                        <div id="airlinesId" name="airlinesId"
                             class="mini-combobox"
                             style="width:250px;"
                             popupWidth="800" textField="name" valueField="id"
                             allowInput="true" required="true" requiredErrorText="航空公司不能为空"
                             emptyText="航空公司名称" onvaluechanged="airlinesIdValueChange">
                            <div property="columns">
                                <div header="名称" field="name"></div>
                                <div header="简称" field="shortName"></div>
                                <div header="两字码" field="twoCode"></div>
                            </div>
                        </div>

                        <div style="margin-top: 5px"></div>
                        <span>班次号：</span>
                        <input id="flightNo" name="flightNo" emptyText="请填写班次号"  class="mini-textbox" style="width: 150px"  required="true" requiredErrorText="班次号不能为空" />
                        <span style="color: red;margin-left: 5px">请直接在两字码后加入航班号</span>

                        <div style="margin-top: 5px"></div>
                        <span>备注:</span>
                        <input id="comment" name="comment" emptyText="请输入备注信息"  class="mini-textbox" style="width: 90%"  required="false" requiredErrorText="备注信息" />


                    </div>
                    <div style="margin-left: 2px;margin-right: 2px"></div>
                    <div style="width: 50%;display: flex;flex-direction:column">
                        <div>
                            <div>起票价设定</div>
                            <span>标准:</span>
                            <input id="ticketPriceAgreementFreightStandardId" name="ticketPriceAgreementFreightStandardId" class="mini-combobox" style="width:120px;" textField="name" valueField="id" emptyText="请选择..."
                                   required="false" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有计费标准"/>

                            <span>类型:</span>
                            <input id="ticketPriceGoodsTypeId" name="ticketPriceGoodsTypeId" class="mini-combobox" style="width:100px;" textField="name" valueField="id" emptyText="请选择..."
                                                     required="false" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择货物类型"/>

                            <span>起票价:</span>
                            <input id="ticketPrice" name="ticketPrice" emptyText="起票价" vtype="float" value="0" class="mini-textbox" style="width: 100px"  required="false" requiredErrorText="起票价不能为空" />

                            <span>备注:</span>
                            <input id="ticketPriceComment" name="ticketPriceComment" emptyText="备注"  class="mini-textbox" style="width: 100px"  required="false" requiredErrorText="起票价备注不能为空" />
                            <a class="mini-button" onclick="addTicketPriceToTicketPriceDatagrid()">添加</a>
                            <a class="mini-button" onclick="removeTicketPriceFromTicketPriceDatagrid()">剔除</a>
                        </div>
                        <div style="margin-top: 5px"></div>
                        <div id="ticketPriceDatagrid" class="mini-datagrid"
                             style="width: 100%; height: 200px;"
                             url="${base}/pages/customer_manage/freight_agreement/ticket_price/queryTicketPriceNoPageList" idField="id"
                             showPager="false"
                             multiSelect = "true" allowResize="false" onload="loadedProcess">
                            <div property="columns">
                                <div type="checkcolumn" ></div>
                                <div field="standardTypeId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">重量标准</div>
                                <div field="goodsTypeId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">货物类型</div>
                                <div field="ticketPrice" width="100" headerAlign="center" align="center" allowSort="true">起票价</div>
                                <div field="comment" width="200" headerAlign="center" align="center" allowSort="true">备注</div>
                            </div>
                        </div>
                    </div>
                </div>

                <div style="margin-top: 5px"></div>

                <div style="display:flex;flex-direction: row;width:100%;height: 340px;">
                    <div style="display: flex;flex-direction:column; justify-content: center; width: 50%;height: 100%">
                        <div>重量运价标准</div>
                        <div style="margin-top: 5px"></div>
                        <div style="display: flex;flex-direction: row;align-items:center;width: 100%">
                            <input id="weightGoodsTypeId" name="weightGoodsTypeId" class="mini-combobox" style="width:100px;" textField="name" valueField="id" emptyText="货物类型..."  required="false" allowInput="false" showNullItem="false" nullItemText="请选择货物类型..." requiredErrorText="没有选择货物类型"/>
                            <span style="margin-right: 2px"></span>
                            <input id="weightStart" name="weightStart" class="mini-textbox"  emptyText="重量起始值" vtype="float" style="width: 80px" />
                            <span style="margin-right: 2px"></span>
                            <input id="weightEnd" name="weightEnd" class="mini-textbox"  emptyText="重量结束值" vtype="float" style="width: 80px" />
                            <span style="margin-right: 2px"></span>
                            <input id="weightPrice" name="weightPrice" class="mini-textbox" emptyText="单价(元/公斤)" vtype="float" style="width: 100px" />
                            <span style="margin-right: 2px"></span>
                            <input id="weightComment" name="weightComment" class="mini-textbox" emptyText="说明" style="width: 80px" />
                            <span class="separator"></span>
                            <a class="mini-button" onclick="addWeightDatagrid()">添加</a>
                            <span style="margin-right: 2px"></span>
                            <a class="mini-button" onclick="deleteWeightDatagrid()">剔除</a>
                        </div>
                        <div style="margin-top: 5px"></div>
                        <div id="weightDatagrid" class="mini-datagrid" style="width:100%;height:285px;"
                             idField="id"
                             showPager="false"
                             allowCellEdit="true" allowCellSelect="true"  multiSelect="true">
                            <div property="columns">
                                <div type="checkcolumn"></div>
                                <div field="goodsTypeId" width="50" headerAlign="center" align="center" data-options='{cacheName:"sysDic"}' renderer="commonRender">货物类型</div>
                                <div field="startValue" width="70" headerAlign="center" align="right" renderer="public_moneyRenderer">重量起始值</div>
                                <div field="endValue" width="70" headerAlign="center" align="right" renderer="public_moneyRenderer">重量结束值</div>
                                <div field="price" width="80" headerAlign="center" align="right" renderer="public_moneyRenderer">单价(元/公斤)</div>
                                <div field="comment" width="140" headerAlign="center" align="left">说明</div>
                                <div field="standardTypeId" width="0">标准类型</div>
                            </div>
                        </div>
                    </div>
                    <div style="margin-left: 2px;margin-right: 2px"></div>
                    <div style="display: flex;flex-direction:column;align-content: center;justify-content: center;width: 50%;height: 100%">
                        <div>体积运价标准</div>
                        <div style="margin-top: 5px"></div>
                        <div style="display: flex;flex-direction: row;align-items:center;width: 100%">
                            <input id="volumeGoodsTypeId" name="volumeGoodsTypeId" class="mini-combobox" style="width:100px;" textField="name" valueField="id" emptyText="货物类型..."  required="false" allowInput="false" showNullItem="false" nullItemText="请选择货物类型..." requiredErrorText="没有选择货物类型"/>
                            <span style="margin-right: 2px"></span>
                            <input id="volumeStart" name="volumeStart" class="mini-textbox"  emptyText="体积起始值" vtype="float" style="width: 80px" />
                            <span style="margin-right: 2px"></span>
                            <input id="volumeEnd" name="volumeEnd" class="mini-textbox"  emptyText="体积结束值" vtype="float" style="width: 80px" />
                            <span style="margin-right: 2px"></span>
                            <input id="volumePrice" name="volumePrice" class="mini-textbox"  emptyText="单价(元/公斤)" vtype="float" style="width: 100px" />
                            <span style="margin-right: 2px"></span>
                            <input id="volumeComment" name="weightComment" class="mini-textbox" emptyText="说明" style="width: 80px" />
                            <span class="separator"></span>
                            <a class="mini-button" onclick="addVolumeDatagrid()">添加</a>
                            <span style="margin-right: 2px"></span>
                            <a class="mini-button" onclick="deleteVolumeDatagrid()">剔除</a>
                        </div>
                        <div style="margin-top: 5px"></div>
                        <div id="volumeDatagrid" class="mini-datagrid" style="width:100%;height:285px;"
                             idField="id"
                             showPager="false"
                             allowCellEdit="true" allowCellSelect="true"  multiSelect="true">
                            <div property="columns">
                                <div type="checkcolumn"></div>
                                <div field="goodsTypeId" width="50" headerAlign="center" align="center" data-options='{cacheName:"sysDic"}' renderer="commonRender">货物类型</div>
                                <div field="startValue" width="70" headerAlign="center" align="right" renderer="public_moneyRenderer">体积起始值</div>
                                <div field="endValue" width="70" headerAlign="center" align="right" renderer="public_moneyRenderer">体积结束值</div>
                                <div field="price" width="80" headerAlign="center" align="right" renderer="public_moneyRenderer">单价(元/公斤)</div>
                                <div field="comment" width="140" headerAlign="center" align="left">说明</div>
                                <div field="standardTypeId" width="0">标准类型</div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

        <!-- 批量上传协议运价 -->
        <div id="uploadPanel" class="mini-window" title="协议运价上传" style="width:400px;height:200px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true" >
            <div property="footer" style="padding:10px;text-align: right">
                <a  id="uploadBtn" name="uploadBtn" class="mini-button" iconCls="icon-upload" onclick="uploadSave()">上传</a>
                <a class="mini-button" iconCls="icon-close" onClick="closePanel('uploadPanel')">关闭</a>
            </div>
            <table style="width: 100%">
                <tr>
                    <td><label>客户协议运价文件:</label></td>
                    <td>
                        <form id="upload-form-add">
                            <input type="file" id="upLoadStandardFileData" name="upLoadFileData"  accept=".xls,.xlsx" />
                        </form>
                    </td>
                </tr>
            </table>
        </div>



        <script type="text/javascript">
            mini.parse();

            let keyDeliveryCustomerId = mini.get("keyDeliveryCustomerId");
            let keyCityId = mini.get("keyCityId");
            let keyAirportId = mini.get("keyAirportId");
            let keyAirlinesId = mini.get("keyAirlinesId");
            let keyFlightNo = mini.get("keyFlightNo");

            let grid = mini.get("datagrid1");

            let airFreightAgreementPanel = mini.get("airFreightAgreementPanel");
            let freightAgreementForm = new mini.Form("#freightAgreementForm");

            let opFlag = mini.get("opFlag");

            let customerId = mini.get("customerId");
            let cityId = mini.get("cityId");
            let airportId = mini.get("airportId");
            let airlinesId = mini.get("airlinesId");
            let flightNo = mini.get("flightNo");
            let comment = mini.get("comment");

            let ticketPriceAgreementFreightStandardId = mini.get("ticketPriceAgreementFreightStandardId");
            let ticketPriceGoodsTypeId = mini.get("ticketPriceGoodsTypeId");
            let ticketPrice = mini.get("ticketPrice");
            let ticketPriceComment = mini.get("ticketPriceComment");
            let ticketPriceDatagrid = mini.get("ticketPriceDatagrid");
            // 新加入的对象要放到该数组中，用于验证是否重复
            let ticketPriceDatagridArray = [];


            // 重量标准
            let weightGoodsTypeId = mini.get("weightGoodsTypeId");
            let weightStart = mini.get("weightStart");
            let weightEnd = mini.get("weightEnd");
            let weightPrice = mini.get("weightPrice");
            let weightComment = mini.get("weightComment");

            let weightDatagrid = mini.get("weightDatagrid");
            let weightArray = [];


            // 体积标准
            let volumeGoodsTypeId = mini.get("volumeGoodsTypeId");
            let volumeStart = mini.get("volumeStart");
            let volumeEnd = mini.get("volumeEnd");
            let volumePrice = mini.get("volumePrice");
            let volumeComment = mini.get("volumeComment");

            let volumeDatagrid = mini.get("volumeDatagrid");
            let volumeArray = [];

            /**
             * 40,0,客户协议运价标准分类,客户协议运价标准分类,XT_DIC_CUSTOMER_AGREEMENT_FREIGHT_STANDARD
             41,40,重量运价标准,重量运价标准,""
             42,40,体积运价标准,体积运价标准,""
             */
            let freightStandard = {weight:41, volume:42};

            // 运输种类
            let transportSpecies ={air:71, auto:72};


            $(function () {
                //  目的市
                let keyCityIdJsonData = commonRenderDropList("city");
                keyCityId.loadList(keyCityIdJsonData,"id","pid");
                cityId.loadList(public_deepClone(keyCityIdJsonData),"id","pid");

                // 始发机场
                let keyAirportIdJsonData = commonRenderDropList("airport");
                keyAirportId.load(keyAirportIdJsonData);
                airportId.load(public_deepClone(keyAirportIdJsonData));

                // 航空公司
                let keyAirlinesIdJsonData = commonRenderDropList("airlines");
                keyAirlinesId.load(keyAirlinesIdJsonData);
                airlinesId.load(public_deepClone(keyAirlinesIdJsonData));

                // 客户加载
                let customerIdJsonData = commonRenderDropList("customer");
                customerId.load(customerIdJsonData);

                // 客户协议运价标准分类AGREEMENT_FREIGHT_STANDARD agreementFreightStandardId
                let agreementFreightStandardIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_CUSTOMER_AGREEMENT_FREIGHT_STANDARD");
                ticketPriceAgreementFreightStandardId.load(agreementFreightStandardIdJsonData);

                // 货物类型加载
                let goodsTypeIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_GOODS_TYPE");
                ticketPriceGoodsTypeId.load(goodsTypeIdJsonData);
                volumeGoodsTypeId.load(public_deepClone(goodsTypeIdJsonData));
                weightGoodsTypeId.load(public_deepClone(goodsTypeIdJsonData));

                // 加载列表数据
                grid.load();
            });

            /**
             * 根据条件进行查询
             */
            function search(){
                let form = new mini.Form("#queryForm");
                let data = form.getData(true);
                grid.load(data);
            }

            /**
             * 清空查询条件
             */
            function clearSearch(){
                let form = new mini.Form("#queryForm");
                form.clear();
            }


            /**
             * 文件下载链接生成
             */
            function fileDownRenderer(e) {
                // if(so.hasPermission("bCustomerAirFreightAgreement:download")){
                //
                // }
                let fileId = e.record.fileId;
                if(public_isEmpty(fileId)){
                    return;
                }
                let htmlStr = '<a  class="mini-button"  style="padding: 2px" onclick="downLoadFile(\'' + fileId + '\')">下载</a>';
                return htmlStr;
            }

            /**
             * 根据fileid进文件下载
             */
            function downLoadFile(fileId) {

                let ret = public_getServerData("${base}/pages/basic_data_manage/xt_upload_file/download",{id:fileId});
                if(!ret.ok){
                    showAlert(ret.message);
                    return;
                }

                // 发送下载请求
                window.location.href="${base}/pages/basic_data_manage/xt_upload_file/filedownload?fileName=" + ret.data;
            }


            /**
             * 显示批量上传面板
             */
            function showUploadPanel() {
                let form_add = new mini.Form("#upload-form-add");
                form_add.clear();
                // 弹出添加窗口
                public_showModelAtPos("uploadPanel", "center", "middle");
            }

            /**
             * 上传模板文件
             */
            function uploadSave() {
                let fileInput = $('#upLoadStandardFileData').get(0).files[0];
                if(fileInput){
                    mini.confirm("您确认上传客户协议运价数据?", "提示", function(confirmAction){
                        if(confirmAction == "ok"){
                            let messageid = mini.loading("正在上传校验及上传处理中，请稍后...","客户协议运价Excel上传");
                            standardAjaxFileUpload(messageid);
                        }
                    });
                }else{
                    mini.alert("没有选择要上传的航班信息数据Excel文件!");
                }
            }

            // 调用异步上传
            function standardAjaxFileUpload(messageid) {
                let formData = new FormData($("#upload-form-add")[0]);
                $.ajax({
                    //接口地址
                    url: '${base}/pages/customer_manage/freight_agreement/air/upload' ,
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (data) {

                        mini.hideMessageBox(messageid);

                        //成功的回调
                        mini.alert(data.message);

                        // 重新刷新数据
                        grid.reload();

                        // 清空上传对象
                        clearUploadFile("upload-form-add");


                        public_onCancel('uploadPanel');

                    },
                    error: function (returndata) {
                        mini.alert("网络异常，请重试！");
                    }
                });
            }


            /**
             * 删除客户运价
             */
            function deleteFreight(){

                let selectItems = grid.getSelecteds();

                if(public_isEmpty(selectItems)){
                    showAlert("请选择需要删除的协议运价!");
                    return;
                }

                let agreementFreightIds = "";
                for(let obj of selectItems){
                    agreementFreightIds += "'" + obj.id + "',";
                }

                // 去除最后一个 "," 号
                agreementFreightIds = agreementFreightIds.substring(0,agreementFreightIds.length-1);


                mini.confirm("确定要删除选择的协议运价？","协议运价删除",function (action) {
                    if (action == "ok") {
                        // 调用删除动作进行删除
                        let ret = public_getServerData("${base}/pages/customer_manage/freight_agreement/air/delete",{agreementFreightIds:agreementFreightIds});
                        showAlert(ret.message,function () {
                            if(ret.ok){
                                grid.reload();
                            }
                        });
                    }
                });
            }

            /**
             * 新增客户运价
             */
            function addFreight(opFlagValue){
                opFlag.setValue(opFlagValue);
                // 先清空
                emptyValue();
                // 面板显示
                public_showModelAtPos("airFreightAgreementPanel", "center", "middle");
            }

            /**
             * 协议运价编辑页面数据初始化与准备
             */
            function editFreight(opFlagValue) {
                let selectItem = grid.getSelected();

                if(public_isEmpty(selectItem)){
                    showAlert("请选择需要编辑的协议运价!");
                    return;
                }

                if(selectItem.length>1){
                    showAlert("编辑协议运价不能选择多行，请选择单行!");
                    return;
                }

                freightAgreementForm.setData(selectItem);

                // 一定要放到freightAgreementForm.setData之后
                opFlag.setValue(opFlagValue);

                // 加载起票价列表
                ticketPriceDatagrid.load({freightAgreementId:selectItem.id});

                // 先清空一下
                emptyDataAndGrid();

                // 加载相应的detail数据，根据agreement_freight_id
                let ret = public_getServerData("${base}/pages/customer_manage/freight_agreement/detail/getDetailListByAgreementFreightId?agreementFreightId=" + selectItem.id);
                if(!ret.ok){
                    showAlert(ret.message);
                    return;
                }

                // 开始分离数据,用于显示
                let allDataList = ret.data;
                for(let obj of allDataList){
                    // 根据standard_type_id 分离是重量还是体积
                    if(obj.standardTypeId == freightStandard.weight){
                        weightArray.push(public_deepClone(obj));
                    }
                    else {
                        volumeArray.push(public_deepClone(obj));
                    }
                }

                // 开始渲染grid
                for(let obj of weightArray){
                    weightDatagrid.addRow(obj);
                }

                for(let obj of volumeArray){
                    volumeDatagrid.addRow(obj);
                }

                // 面板初始化
                public_showModelAtPos("airFreightAgreementPanel", "center", "middle");
            }

            /**
             * 航空公司选择
             */
            function airlinesIdValueChange(e) {
                flightNo.setValue(e.selected.twoCode);
            }


            /**
             * 保存信息
             */
            function saveAirFreightAgreement() {

                freightAgreementForm.validate();
                if (freightAgreementForm.isValid() == false) {
                    return;
                }

                // 对需要保存的列表进行序列校验
                // 1.加载货物类型
                let weightGoodsTypeIdJsonDataArray = commonRenderSysDicDropListByKey("sysDic","XT_DIC_GOODS_TYPE");

                // 重量运价明细连续性检测
                for(let obj of weightGoodsTypeIdJsonDataArray){
                    let goodsTypeName = obj.name;
                    let tempArray = [];
                    for(let weightObj of weightArray){
                        if(obj.id == weightObj.goodsTypeId){
                            tempArray.push(weightObj);
                        }
                    }
                    if(tempArray.length>0){
                        let flag = isSequence(tempArray);
                        if(!flag){
                            showAlert("重量运价明细【" + goodsTypeName +  "】类型中起始值与结束值的连续性不符合要求或该类型的起始值不为0!")
                            return;
                        }
                    }
                }


                // 体积运价明细连续性检测
                for(let obj of weightGoodsTypeIdJsonDataArray){
                    let goodsTypeName = obj.name;
                    let tempArray = [];
                    for(let weightObj of  volumeArray){
                        if(obj.id == weightObj.goodsTypeId){
                            tempArray.push(weightObj);
                        }
                    }
                    if(tempArray.length>0){
                        let flag = isSequence(tempArray);
                        if(!flag){
                            showAlert("体积运价明细【" + goodsTypeName +  "】类型中起始值与结束值的连续性不符合要求或该类型的起始值不为0!")
                            return;
                        }
                    }
                }

                // 至些数据检测通过，可以入库
                let formData = freightAgreementForm.getData(true);

                if(weightArray.length>0 || volumeArray.length>0){

                    let detailPojos = [];
                    detailPojos.push(...weightArray);
                    detailPojos.push(...volumeArray);

                    // 这是明细表数据
                    formData.detailPojos = detailPojos;
                    // 这是起票价数据
                    formData.ticketPricePojos = ticketPriceDatagridArray;

                    // b_customer_air_freight_agreement(这是主表)
                    // b_customer_freight_agreement_detail(这是明细表)
                    // b_customer_freight_ticket_price(这是起票价表)
                    let saveUrl = "${base}/pages/customer_manage/freight_agreement/air/saveAirAgreement";
                    let ret = public_getServerData(saveUrl, formData);
                    if(ret.ok){
                        showAlert(ret.message,function (){
                            // 刷新grid
                            grid.reload();
                            // 关闭窗口
                            closePanel('airFreightAgreementPanel');
                        });
                    }
                    else{
                        showAlert(ret.message,function (){
                            return;
                        });
                    }
                }
                else{
                    showAlert("重量运价标准与体积运价标准都为空，不能进行保存操作!",function (){
                        return;
                    });
                }
            }

            function closePanel(panelId) {
                // 关闭面板
                public_onCancel(panelId);
            }


            // 关闭或保存后的数据清空
            function emptyValue() {
                opFlag.setValue();
                customerId.setValue();
                cityId.setValue();
                airportId.setValue();
                airlinesId.setValue();
                flightNo.setValue();
                comment.setValue();

                weightGoodsTypeId.setValue();
                weightStart.setValue();
                weightEnd.setValue();
                weightPrice.setValue();
                weightComment.setValue();

                volumeGoodsTypeId.setValue();
                volumeStart.setValue();
                volumeEnd.setValue();
                volumePrice.setValue();
                volumeComment.setValue();

                emptyDataAndGrid();

            }

            /**
             * 清空列表及数组
             */
            function emptyDataAndGrid() {
                volumeArray.splice(0,volumeArray.length);
                //清空所有行
                volumeDatagrid.clearRows();

                weightArray.splice(0,weightArray.length);
                //清空所有行
                weightDatagrid.clearRows();

                ticketPriceDatagridArray.splice(0,ticketPriceDatagridArray.length);
                ticketPriceDatagrid.clearRows();
            }


            /**
             * 添加重量分类协议运价
             */
            function addWeightDatagrid(){
                if(isEmpty(weightGoodsTypeId,"货物类型")){
                    return;
                }

                if(isEmpty(weightStart,"重量起始值")){
                    return;
                }

                if(isEmpty(weightEnd,"重量结束值")){
                    return;
                }

                if(isEmpty(weightPrice,"重量单价")){
                    return;
                }

                if(isNonNumeric(weightStart,"重量起始值")){
                    return;
                }

                if(isNonNumeric(weightEnd,"重量结束值")){
                    return;
                }

                if(isNonNumeric(weightPrice,"重量单价")){
                    return;
                }

                let weightGoodsTypeIdValue = weightGoodsTypeId.getValue();
                let weightStartValue = public_toNumber(weightStart.getValue());
                let weightEndValue = public_toNumber(weightEnd.getValue());
                let weightPriceValue = public_toNumber(weightPrice.getValue());
                let commentValue = weightComment.getValue();
                let standardTypeIdValue = freightStandard.weight;


                if(weightEndValue<=0){
                    showAlert("重量结束值不能小于或等于零!");
                    return;
                }
                else if(weightEndValue<=weightStartValue){
                    showAlert("重量结束值不能小于或等于重量起始值!");
                    return;
                }
                if(weightPriceValue<=0){
                    showAlert("重量单价不能小于或等于零!");
                    return;
                }

                // 生成新添加的对象
                let newRow = {goodsTypeId:weightGoodsTypeIdValue, startValue:weightStartValue, endValue:weightEndValue, price:weightPriceValue, comment:commentValue,standardTypeId:standardTypeIdValue, agreementFreightTypeId:transportSpecies.air };

                // 检测是否有重复值存在
                let isExist = checkRepetition(weightArray,newRow);
                if(isExist){
                    showAlert("新增加的重量协议运价明细已经存在，不能添加该明细!");
                    return;
                }

                // 不重复就可以插入，插入后进行排序，然后输出
                weightArray.push(newRow);

                // 下面排序
                weightArray.sort(sortData);

                //清空所有行
                weightDatagrid.clearRows();
                // 重新绑定
                for(let obj of weightArray){
                    weightDatagrid.addRow(obj);
                }
            }

            function deleteWeightDatagrid(){
                let selectedItemArray = weightDatagrid.getSelecteds();

                if(selectedItemArray.length==0){
                    showAlert("没有选择要剔除的重量运价标准明细!");
                    return;
                }

                for(let selectedObj of selectedItemArray){
                    for(let i=weightArray.length-1;i>=0;i--){
                        let obj = weightArray[i];
                        let flag = equalObj(obj,selectedObj);
                        if(flag){
                            // 剔除指定位置的元素
                            weightArray.splice(i,1);
                            break;
                        }
                    }
                }

                // 排序
                weightArray.sort(sortData);

                //清空所有行
                weightDatagrid.clearRows();
                // 重新渲染数据
                for(let obj of weightArray){
                    weightDatagrid.addRow(obj);
                }
            }

            /**
             * 添加体积分类协议运价
             */
            function addVolumeDatagrid() {
                if(isEmpty(volumeGoodsTypeId,"货物类型")){
                    return;
                }

                if(isEmpty(volumeStart,"体积起始值")){
                    return;
                }

                if(isEmpty(volumeEnd,"体积结束值")){
                    return;
                }

                if(isEmpty(volumePrice,"体积单价")){
                    return;
                }

                if(isNonNumeric(volumeStart,"体积起始值")){
                    return;
                }

                if(isNonNumeric(volumeEnd,"体积结束值")){
                    return;
                }

                if(isNonNumeric(volumePrice,"体积单价")){
                    return;
                }

                let volumeGoodsTypeIdValue = volumeGoodsTypeId.getValue();
                let volumeStartValue = public_toNumber(volumeStart.getValue());
                let volumeEndValue = public_toNumber(volumeEnd.getValue());
                let volumePriceValue = public_toNumber(volumePrice.getValue());
                let commentValue = volumeComment.getValue();
                let standardTypeIdValue = freightStandard.volume;

                if(volumeEndValue <= 0){
                    showAlert("重量结束值不能小于或等于零!");
                    return;
                }
                else if(volumeEndValue <= volumeStartValue){
                    showAlert("重量结束值不能小于或等于重量起始值!");
                    return;
                }
                if(volumePriceValue <= 0){
                    showAlert("重量单价不能小于或等于零!");
                    return;
                }

                // 生成新添加的对象
                let newRow = {goodsTypeId:volumeGoodsTypeIdValue, startValue:volumeStartValue, endValue:volumeEndValue, price:volumePriceValue, comment:commentValue,standardTypeId:standardTypeIdValue, agreementFreightTypeId:transportSpecies.air};

                // 检测是否有重复值存在
                let isExist = checkRepetition(volumeArray,newRow);
                if(isExist){
                    showAlert("新增加的体积协议运价明细已经存在，不能添加该明细!");
                    return;
                }

                // 不重复就可以插入，插入后进行排序，然后输出
                volumeArray.push(newRow);

                // 下面排序
                volumeArray.sort(sortData);

                //清空所有行
                volumeDatagrid.clearRows();
                // 重新绑定
                for(let obj of volumeArray){
                    volumeDatagrid.addRow(obj);
                }
            }


            function deleteVolumeDatagrid() {
                let selectedItemArray = volumeDatagrid.getSelecteds();

                if(selectedItemArray.length==0){
                    showAlert("没有选择要剔除的体积运价标准明细!");
                    return;
                }

                for(let selectedObj of selectedItemArray){
                    for(let i=volumeArray.length-1;i>=0;i--){
                        let obj = volumeArray[i];
                        let flag = equalObj(obj,selectedObj);
                        if(flag){
                            // 删除指定位置的元素
                            volumeArray.splice(i,1);
                            break;
                        }
                    }
                }

                // 排序
                volumeArray.sort(sortData);

                //清空所有行
                volumeDatagrid.clearRows();
                // 重新渲染数据
                for(let obj of volumeArray){
                    volumeDatagrid.addRow(obj);
                }

            }

            /**
             * 类型及开始与结束值相等就可以了
             */
            function equalObj(obj1,obj2){
                return obj1.goodsTypeId === obj2.goodsTypeId && obj1.startValue === obj2.startValue && obj1.endValue === obj2.endValue;
            }

            /**
             * 对象根据类型及起始值进行排序
             * @param obj1
             * @param obj2
             * @returns {number}
             */
            function sortData(obj1, obj2) {
                if(obj1.goodsTypeId==obj2.goodsTypeId){
                    if(obj1.startValue>obj2.startValue){
                        return 1;
                    }
                    else if(obj1.startValue<obj2.startValue){
                        return -1;
                    }
                    else {
                        return 0;
                    }
                }
                else{
                    if(obj1.goodsTypeId>obj2.goodsTypeId){
                        return 1;
                    }
                    else{
                        return -1;
                    }
                }
            }

            /**
             * 序列完整性检测
             * @param checkArray
             * @returns {boolean}
             */
            function isSequence(checkArray){
                let flag = true;
                if(checkArray.length==1){
                    // 第一行强制要求起始值必须为零
                    if(checkArray[0].startValue!=0){
                        flag = false;
                        return flag;
                    }
                }

                for(let i=0;i<checkArray.length -1;i++){
                    let value1 = checkArray[i].endValue;
                    let value2 = checkArray[i+1].startValue;
                    if(i==0){
                        // 第一行强制要求起始值必须为零
                        if(checkArray[i].startValue!=0){
                            flag = false;
                            break;
                        }
                    }
                    if(value1!=value2){
                        flag = false;
                        break;
                    }
                }
                return flag;
            }

            /**
             * 检测对象在数组对象中是否已经存在
             * @param array
             * @param item
             * @returns {boolean}
             */
            function checkRepetition(array,item) {
                let repetitionFlag = false;
                for(let obj of array){
                    repetitionFlag = equalObj(obj,item);
                    if(repetitionFlag){
                        break;
                    }
                }
                return repetitionFlag;
            }

            /**
             * 起票价 datagrid 加载数据完成后的处理
             */
            function loadedProcess(e){
                let dataArray = e.result.data;
                if(dataArray.length>0){
                    for(let obj of dataArray){
                        let newRow = {goodsTypeId:obj.goodsTypeId, ticketPrice:obj.ticketPrice, comment:obj.comment};
                        ticketPriceDatagridArray.push(obj);
                    }
                }
            }

            /**
             * 添加协议运价的起票价
             */
            function addTicketPriceToTicketPriceDatagrid(){
                // 起票价运输标准
                let ticketPriceAgreementFreightStandardIdValue = ticketPriceAgreementFreightStandardId.getValue();
                if(public_isEmpty(ticketPriceAgreementFreightStandardIdValue)){
                    showAlert("没有选择起票价运价标准!");
                    return;
                }

                let ticketPriceGoodTypeIdValue = ticketPriceGoodsTypeId.getValue();
                if(public_isEmpty(ticketPriceGoodTypeIdValue)){
                    showAlert("没有选择起票价类型!");
                    return;
                }


                let ticketPriceValue = ticketPrice.getValue();
                if(!public_isNumber(ticketPriceValue) || ticketPriceValue<=0){
                    showAlert("起票价不是数值或起票价不大于零!");
                    return;
                }

                let ticketPriceCommentValue = ticketPriceComment.getValue();

                let newRow = {standardTypeId:ticketPriceAgreementFreightStandardIdValue, goodsTypeId:ticketPriceGoodTypeIdValue, ticketPrice:ticketPriceValue, comment:ticketPriceCommentValue};

                // 检测在数组中是否存在
                let isExist=false;
                for(let obj of ticketPriceDatagridArray){
                    if(obj.standardTypeId == newRow.standardTypeId && obj.goodsTypeId == newRow.goodsTypeId){
                        isExist=true;
                        break;
                    }
                }

                if(isExist){
                    showAlert("添加的【" + ticketPriceAgreementFreightStandardId.getText() + "】【" + ticketPriceGoodsTypeId.getText() + "】起票价已经在列表中存在，不能重复添加!");
                    return;
                }

                // 加入到数组
                ticketPriceDatagridArray.push(newRow);
                redrawDatagrid();
            }

            /**
             * 剔除相应起票价类型
             */
            function removeTicketPriceFromTicketPriceDatagrid(){
                let selectedItems = ticketPriceDatagrid.getSelecteds();
                if(selectedItems.length==0){
                    showAlert("没有选择需要剔除的起票价!");
                    return;
                }

                let tempArray = [];
                for(let obj of ticketPriceDatagridArray){
                    let isFound=false;
                    for(let obj1 of selectedItems){
                        if(obj.standardTypeId == obj1.standardTypeId && obj.goodsTypeId==obj1.goodsTypeId && obj.ticketPrice == obj1.ticketPrice){
                            isFound=true;
                            break;
                        }
                    }
                    if(!isFound){
                        tempArray.push(obj);
                    }
                }
                // 清空
                ticketPriceDatagridArray.splice(0,ticketPriceDatagridArray.length);
                ticketPriceDatagridArray.push(...tempArray);

                redrawDatagrid();
            }

            // 重新渲染
            function redrawDatagrid(){
                // 清空
                ticketPriceDatagrid.clearRows();
                // 重新添加
                for(let obj of ticketPriceDatagridArray){
                    ticketPriceDatagrid.addRow(obj);
                }
            }


            /**
             * 保存客户协议对应的起票价
             */
            // function saveTicketPrice(){
            //     let freightAgreementIdValue = ticketPriceFreightAgreementId.getValue();
            //
            //     if(public_isEmpty(freightAgreementIdValue)){
            //         showAlert("协议运价ID不存在，不能进行保存起票价操作，请重试!");
            //         return;
            //     }
            //
            //     if(ticketPriceDatagridArray.length==0){
            //         showAlert("起票价列表为空，不能进保存操作!");
            //         return;
            //     }
            //
            //     mini.confirm("确定保存起票价列表?", "起票价保存", function(confirmAction){
            //         if(confirmAction == "ok"){
            //             let ret = public_getServerData("${base}/pages/customer_manage/freight_agreement/ticket_price/batchSave",{freightAgreementId:freightAgreementIdValue,ticketPriceArray:ticketPriceDatagridArray});
            //             showAlert(ret.message,function (){
            //                 if(ret.ok){
            //                     // 关闭当前面板
            //                     closePanel('ticketPricePanel');
            //                 }
            //             });
            //         }
            //     });
            // }

        </script>

    </body>
</html>